﻿using System;
using System.Console;
using System.IO;
using System.Text;
using SCG = System.Collections.Generic;
using Xml = System.Xml;

using Nemerle.Xml.ShortNames;

module Program
{
  Main() : void
  {
    def template =
      DDoc([DProcInstr("xml", "version = \"1.0\" encoding=\"utf-8\""),
                DProcInstr("xsl-stylesheet", "version = \"1.0\" encoding=\"utf-8\"")], [
        DElem(DNUri("http ://msdn.microsoft.com/"), "", "html", [DAttr("xmlns:x","somens")], [
          DElem("title", [DAttr(DNUri("test-ns"), "x", "test-atr5", "test value5")], [DText("lalala\nTitle\nkos")]),
          DElem("title2", [], [DText("Title2")]),
          DElem(DNUri("test-ns"), "x", "body", [DAttr("xmlns", "http ://www.nemerle.org")], [
            DElem("p", [], [DText("Hellow, world!")]),
            DElem("table", [DAttr("atr", "attr value")], []),
            DElem("subNode", [], [DElem("subSubNode", [], [DText("sub")])]),
            DElem("order", [DAttr("att", "12"), DAttr("br", "55")], [DText("cool")])
          ])
        ])]);

    mutable xml = template;

//    WriteLine(xml);

    def transform(el : DElemBase)
    {
      //| DElem("body", _, _) as el => DElem("body-element", el.attrs, el.nodes);
      //| DElem("body-element", _, _) as el => DElem("wow", el.attrs, []);
      //| DElem("html", attrs, nodes) => DElem("root", attrs + [ ("a", "2"), ("b", "4") ], nodes);
      | DElem(_, _, "body", _, _) => null
      | DElem(ns, prefix, name, a, n) => DElem(ns, prefix, name, a, n)
      | DProcInstr(_, _) as pi => DProcInstr(pi.name, "")
      | _ => el
    }

//    xml = xml.Transform(transform) :> Document;
    WriteLine(xml);

    WriteLine( xml.Transform(transform) );
    WriteLine( xml.Equals(xml.Transform(transform)) );

    WriteLine( xml.Equals(template) );

//    WriteLine("\n----DOM----\n");

    def dom = DContext.Default.FromString(xml.ToString());

    //
    WriteLine("\n----DomWriter----\n");
    ////
    Console.WriteLine(DContext.Default.ToString(dom));
    //
    //WriteLine("\n");

//    writer.Formatting = XML.Formatting.Indented;
//    DomWriter.Write(DomReader.Read( XML.XmlTextReader("http ://www.rsdn.ru/Forum/opml.aspx") ), writer);

//    def dom2 = DomReader.Read( XML.XmlTextReader( System.IO.MemoryStream(System.DText.Encoding.UTF8.GetBytes(dom.ToString())) ) );
//    WriteLine(dom2);

    //def xmlDoc = XML.XmlDocument();
    //xmlDoc.Load(XML.XmlTextReader("http ://www.rsdn.ru/Forum/opml.aspx"));
    //
//    def dom = DomReader.Read( XML.XmlTextReader("http ://www.rsdn.ru/Forum/opml.aspx") );
//    WriteLine(dom.ToString() == xmlDoc.OuterXml);
//    WriteLine(dom);

    //def reader = XML.XmlTextReader( MemoryStream(Encoding.UTF8.GetBytes("<?xml version=\"1.0\" encoding=\"utf-8\"?>")) ) ;
    //while (reader.Read())
    //{
    //  def _v = reader.Value;
    //}

    _ = ReadKey();
  }
}